<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //源数据
        let person = {
            name:"张三",
            age:18,
        }

        //模拟vue2中实现响应式
        //let p = {};
        /* Object.defineProperty(p,"name",{
            get(){
                return person.name;
            },
            set(value){
                console.log("有人修改了name属性，发现后要去更新界面");
                person.name = value;
            },
        });
        
        Object.defineProperty(p,"age",{
            get(){
                return person.age;
            },
            set(value){
                console.log("有人修改了agee属性，发现后要去更新界面");
                person.age = value;
            },
        });
 */

        //模拟vue3中实现响应式  
        let p = new Proxy(person,{
            //读取
            get(target,propName){
                // return target[propName];
                return Reflect.get(target,propName);
            },
            //修改，增加
            set(target,propName,value){
                console.log(`有人修改了${target}身上的${propName}`);
                // target[propName] = value;
                Reflect.set(target,propName,value);
            },
            //删除
            deleteProperty(target,propName){
                console.log(`有人删除了${target}身上的${propName}`);
                // return delete target[propName];
                return Reflect.delete(target,propName);
            }
        });

        let obj = {
            a:1,
            b:2,
        };

        /* //会报错，不能重复定义同一个属性
        Object.defineProperty(obj,"c",{
            get(){
                return 3;
            }
        });

        Object.defineProperty(obj,"c",{
            get(){
                return 4;
            }
        }); */

        //可以重复定义相同属性,但是返回值只有第一次为true，之后为false，结果只人第一次的，对于封装有好处，不用再try catch
        /* const x1=Reflect.defineProperty(obj,"c",{
            get(){
                return 3;
            }
        });

        const x2=Reflect.defineProperty(obj,"c",{
            get(){
                return 4;
            }
        }); 

        console.log(x1);
        console.log(x2); */

    </script>
</body>
</html>